package aqua.smile.springboot.security.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * 用户信息
 *
 * @author chujian
 * @since 2022-11-08 22:16
 **/
@RequestMapping("/userInfo")
@RestController
@Slf4j
public class UserInfoController {

    @GetMapping("/t1")
    public Object t1(Principal principal) {
        return principal;
    }

    @GetMapping("/t2")
    public Object t2(Authentication authentication) {
        return authentication;
    }

    @GetMapping("/t3")
    public Object t3(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        return principal;
    }

    @GetMapping("t4")
    public Object t4() {
        Object principal = SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal();
        return principal;
    }

}
